package helen.core.tools.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import android.os.Environment;

/**
 * 
  * @name FileUtils
  * @author 原作者
  *	@description 对安卓进行文件访问的类
 */
public class FileUtils {

	public String sdCardRoot;

	public FileUtils() {
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			sdCardRoot = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + File.separator;
		}
		System.out.println(sdCardRoot);
	}


	public File createFileInSdCard(String fileName, String dir)
			throws IOException {
		File file = new File(sdCardRoot + dir + File.separator + fileName);
		file.createNewFile();
		return file;
	}


	public File createSdDir(String dir) {
		File dirFile = new File(sdCardRoot + dir + File.separator);
		dirFile.mkdir();
		return dirFile;
	}


	public boolean isFileEixt(String path, String fileName) {
		File file = null;
		if (path == null) {
			file = new File(sdCardRoot + fileName);
		} else {
			file = new File(sdCardRoot + path + File.separator + fileName);
		}
		return file.exists();
	}

	
	public File write2SdFromInput(String path, String fileName,
			InputStream input) {
		File file = null;
		OutputStream outputStream = null;
		createSdDir(path);
		try {
			file = createFileInSdCard(fileName, path);
			outputStream = new FileOutputStream(file);
			byte buffer[] = new byte[4 * 1024];
			int temp;
			while ((temp = input.read(buffer)) != -1) {
				outputStream.write(buffer, 0, temp);
			}
			outputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;
	}

	public InputStream File2InputSream(String path, String fileName){
		File file = null;
		if (path == null) {
			file = new File(sdCardRoot + fileName);
		} else {
			file = new File(sdCardRoot + path + File.separator + fileName);
		}
		
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return inputStream;
	}
	
	public String readFromInput(String path, String fileName) {
		File file = null;
		StringBuffer sb = new StringBuffer();
		FileInputStream fileIS = null;
		try {
			if (path == null) {
				file = new File(sdCardRoot + fileName);
			} else {
				file = new File(sdCardRoot + path + File.separator + fileName);
			}

			String absolutepath = file.getAbsolutePath();
			System.out.println("path absolute-->" + absolutepath + " exites->"
					+ file.exists());
			if (file.exists()) {
				fileIS = new FileInputStream(absolutepath);
				BufferedReader buf = new BufferedReader(new InputStreamReader(
						fileIS));
				String readString = new String();
				// just reading each line and pass it on the debugger
				while ((readString = buf.readLine()) != null) {
					sb.append(readString);
				}
				fileIS.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (fileIS != null) {
					fileIS.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public InputStream String2InputStream(String str){
		   ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
		   return stream;
	}
	
	String inputStream2String(InputStream is){
		   BufferedReader in = new BufferedReader(new InputStreamReader(is));
		   StringBuffer buffer = new StringBuffer();
		   String line = "";
		   try {
			while ((line = in.readLine()) != null){
			     buffer.append(line);
			   }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		   return buffer.toString();
	}

	public String getAbusName(String path, String fileName) {
		if(path==null){
			return sdCardRoot+fileName;
		}else{
			return sdCardRoot + path + File.separator + fileName;
		}
	}

	public String getAbusPath(String path) {
		if(path==null){
			return sdCardRoot;
		} else{
			return sdCardRoot + path;
		}
	}
}
